﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
using GomeeSoft.src.classes;
using GomeeSoft.src.form.Popup;

namespace GomeeSoft.src.form.DiaryPages
{
    public partial class UCLoginR : UserControl
    {
        public UCLoginR()
        {
            InitializeComponent();
            InitializeDesign();
            InitializeData();
        }

        private void InitializeData()
        {
            SQLiteQuery query = new SQLiteQuery();
            query.SetCommand(GomeeSoft.Properties.Resources.SQLUserListSelect);
            DataTable dataTable = new DataTable();
            dataTable = query.SelectDataTable();

            if (dataTable != null)
            {
                foreach (DataRow dataRow in dataTable.Rows)
                {
                    cb_user.AddItem(dataRow.Field<string>("user_id"));
                }
            }
        }

        private void InitializeDesign()
        {
            BackColor = Color.White;
            lb_application.Font = new Font(Global.FontTahoma, 14, FontStyle.Regular, GraphicsUnit.Pixel);
            lb_title.Font = new Font(Global.FontTahoma, 32, FontStyle.Regular, GraphicsUnit.Pixel);
            lb_license.Font = new Font(Global.FontTahoma, 14, FontStyle.Regular, GraphicsUnit.Pixel);
            lb_version.Font = new Font(Global.FontTahoma, 14, FontStyle.Regular, GraphicsUnit.Pixel);
            lb_application.ForeColor = Global.GetColor(Global.UCLoginApplicationTextColor);
            lb_title.ForeColor = Global.GetColor(Global.UCLoginTitleTextColor);
            lb_license.ForeColor = lb_title.ForeColor;
            lb_version.ForeColor = lb_title.ForeColor;
        }

        private void UCLoginR_Load(Object sender, EventArgs e)
        {
            cb_user.Focus();
        }

        private void UCLoginR_Resize(object sender, EventArgs e)
        {
            // Title
            Point p = new Point((Width - lb_application.Width) / 2, 102);
            lb_application.Location = p;
            p = new Point((Width - lb_title.Width) / 2, 125);
            lb_title.Location = p;
            // License
            p.Offset(lb_title.Width - 10, lb_title.Height - 20);
            lb_version.Location = p;
            p = new Point((Width - lb_license.Width) / 2, 187);
            lb_license.Location = p;
            // User ID Label
            p = new Point(37, Height - 101);
            lb_userid.Location = p;
            // User Combo
            p.Offset(lb_userid.Width + 1, 0);
            cb_user.Location = p;
            // New User Button
            p = new Point(Width - 85, Height - 101);
            bt_newuser.Location = p;
            // Password Label
            p = new Point(37, Height - 82);
            lb_password.Location = p;
            // Button TextBox Label
            p.Offset(lb_userid.Width + 1, 0);
            tb_password.Location = p;
            p = new Point(Width - 85, Height - 82);
            // Login button
            bt_login.Location = p;
            cb_user.Width = bt_newuser.Left - cb_user.Left - 1;
            tb_password.Width = bt_newuser.Left - cb_user.Left - 1;
        }

        private void bt_newuser_ButtonClick(object sender, MouseEventArgs e)
        {
            PUAddUser popup = new PUAddUser();

            if (popup.ShowDialog(this) == DialogResult.OK)
            {
                cb_user.Text = popup.UserID;
                cb_user.AddItem(cb_user.Text);
            }
            popup.Dispose();
        }

        private void bt_login_ButtonClick(object sender, MouseEventArgs e)
        {
            ConfirmAuthentication();
        }

        private void ConfirmAuthentication()
        {
            MD5 md5 = MD5.Create();
            byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(tb_password.Text);
            byte[] hash = md5.ComputeHash(inputBytes);

            SQLiteQuery query = new SQLiteQuery();
            String sqlQuery = "select * from tb_sys_user where user_id = @user_id and password = @hash_password";
            query.SetCommand(sqlQuery);
            query.SetParameter("@user_id", cb_user.Text);
            query.SetParameter("@hash_password", hash);
            DataTable dataTable =  query.SelectDataTable();

            if (dataTable.Rows.Count == 0)
            {
                MessageBox.Show("User not found of password incorrect");
                return;
            }

            OnLoggedIn(EventArgs.Empty);
        }

        private void lb_password_KeyDown(Object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                ConfirmAuthentication();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public delegate void EventHandler(object sender, EventArgs e);
        public event EventHandler LoggedIn;
        protected virtual void OnLoggedIn(EventArgs e)
        {
            LoggedIn(this, e);
        }
    }
}
